<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索 添加 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入课程名称" v-model="queryInfo.query" clearable @clear="getCourseList" @keydown.enter.native="getOneByClassId">
            <el-button slot="append" icon="el-icon-search" @click="getOneByClassId"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addCourseDialogVisible = true">添加课程</el-button>
        </el-col>
      </el-row>
      <!-- 课程列表区域 -->
      <el-table :data="courseList" border stripe>
        <!-- stripe: 斑马条纹
        border：边框-->
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="courseId" label="课程编号"></el-table-column>
        <el-table-column prop="courseName" label="课程名称"></el-table-column>
        <el-table-column prop="college" label="所属院系"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="showEditDialog(scope.row.courseId)"></el-button>
            <!--删除按钮-->
            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="removeById(scope.row.courseId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <!-- <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-size="queryInfo.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination> -->
    </el-card>

    <!-- 添加课程的对话框 -->
    <el-dialog title="添加课程" :visible.sync="addCourseDialogVisible" width="30%" @close="addDialogClosed" @keydown.enter.native="add">
      <!-- 内容主体 -->
      <el-form :model="addCourseForm" ref="addCourseFormRef" :rules="addCourseFormRules" label-width="100px">
        <el-form-item label="课程编码" prop="courseId">
          <el-input v-model="addCourseForm.courseId"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="addCourseForm.courseName"></el-input>
        </el-form-item>

        <el-form-item label="所属院系" prop="college">
          <el-select v-model="addCourseForm.college" placeholder="请选择">
            <!-- <el-option
              v-for="item in deptList"
              :key="item.courseId"
              :label="item.deptName"
              :value="item.courseId"
            > 
            </el-option>-->
            <el-option label="计算机" value="计算机" college="计算机"></el-option>
            <el-option label="机械" value="机械" college="机械"></el-option>
            <el-option label="服装" value="服装" college="服装"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCourseDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改课程的对话框 -->
    <el-dialog title="修改课程信息" :visible.sync="editCourseDialogVisible" width="50%" @close="editDialogClosed" @keydown.enter.native="editUser">
      <!-- 内容主体 -->
      <el-form :model="editForm" ref="editFormRef" :rules="editFormRules" label-width="70px">
        <el-form-item label="课程编码">
          <el-input v-model="editForm.courseId" disabled></el-input>
        </el-form-item>
        <el-form-item label="课程名称">
          <el-input v-model="editForm.courseName"></el-input>
        </el-form-item>
        <!-- TODO  -->
        <el-form-item label="所属院系" prop="college">
          <el-select v-model="editForm.college" placeholder="请选择">
            <!-- <el-option
              v-for="item in deptList"
              :key="item.id"
              :label="item.deptName"
              :value="item.id"
            >
            </el-option> -->
            <el-option label="计算机" value="计算机" college="计算机"></el-option>
            <el-option label="机械" value="机械" college="机械"></el-option>
            <el-option label="服装" value="服装" college="服装"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCourseDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      courseList: [],
      // 院系列表
      deptList: [],
      // 获取课程列表查询参数对象
      queryInfo: {
        query: ''
        // 当前页数
        //pageNo: 1,
        // 每页显示多少数据
        //pageSize: 5
      },
      // 添加课程对话框的显示与隐藏
      addCourseDialogVisible: false,
      // 修改课程对话框的显示与隐藏
      editCourseDialogVisible: false,
      // 课程添加
      addCourseForm: {
        courseId: '',
        courseName: '',
        college: ''
      },
      // 课程添加表单验证规则
      addCourseFormRules: {
        courseId: [
          { required: true, message: '请输入课程编号', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '课程编号的长度在2～10个字',
            trigger: 'blur'
          }
        ],
        courseName: [
          { required: true, message: '请输入课程名称', trigger: 'blur' },
          {
            min: 2,
            max: 50,
            message: '课程名称的长度在2～50个字',
            trigger: 'blur'
          }
        ],
        college: [{ required: true, message: '请输入所属院系', trigger: 'blur' }]
      },
      // 修改课程
      //editCourseDialogVisible: false,
      editForm: {},
      // 编辑课程表单验证
      editFormRules: {
        courseId: [{ required: true, message: '请输入课程编号', trigger: 'blur' }],
        courseName: [{ required: true, message: '请输入课程名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getCourseList()
    // 加载院系列表
    this.getDeptList()
  },
  methods: {
    async getCourseList() {
      console.log('getCourseList被调用')
      const { data: res } = await this.$axios.get('/api/admin/getCurriculumAll')
      console.log(res)
      if (!res) {
        return this.$message.error('获取课程列表失败！')
      }
      // this.courseList = res.data.records
      // this.total = res.data.total
      this.$message.success('获取用户列表成功')
      this.courseList = res
    },
    async getDeptList() {
      const { data: res } = await this.$axios.get('/api/admin/getCurriculumAll')
      if (!res) {
        return this.$message.error('获取院系列表失败！')
      }
      this.deptList = res
    },
    // 监听 添加课程对话框的关闭事件
    addDialogClosed() {
      //this.$refs.addCourseFormRef.resetFields()
    },
    // 添加课程
    add() {
      // 提交请求前，表单预验证
      this.$refs.addCourseFormRef.validate(async (valid) => {
        // console.log(valid)
        // 表单预校验失败
        if (!valid) return
        const { data: res } = await this.$axios.post('/api/admin/newCurriculum', this.addCourseForm).catch((err) => err)
        console.log(res)
        if (res !== 1) {
          return this.$message.error('添加课程失败！')
        }
        this.$message.success('添加课程成功！')
        // 隐藏添加课程对话框
        this.addCourseDialogVisible = false
        this.getCourseList()
      })
    },
    // 编辑课程信息
    async showEditDialog(courseId) {
      this.editCourseDialogVisible = true
      const { data: res } = await this.$axios.get('/api/admin/getCurriculum', {
        params: {
          courseId
        }
      })
      console.log(res)
      if (!res) {
        return this.$message.error('查询课程信息失败！')
      }
      this.editForm = res
      this.editCourseDialogVisible = true
    },
    // 监听修改课程对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改课程信息
    editUser() {
      // 提交请求前，表单预验证
      this.$refs.editFormRef.validate(async (valid) => {
        // console.log(valid)
        // 表单预校验失败
        if (!valid) return
        const { data: res } = await this.$axios.post(
          '/api/admin/getNewCurriculum',
          this.editForm
          // {
          //   id: this.editForm.id,
          //   courseId: this.editForm.courseId,
          //   courseName: this.editForm.courseName,
          //   college: this.editForm.college
          // }
        )
        console.log(res)
        if (res !== 1) {
          this.$message.error('更新课程信息失败！')
        }
        // 隐藏添加课程对话框
        this.editCourseDialogVisible = false
        this.$message.success('更新课程信息成功！')
        this.getCourseList()
      })
    },
    // 删除课程
    async removeById(courseId) {
      console.log('courseId', courseId)
      const confirmResult = await this.$confirm('确定删除该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      // 点击确定 返回值为：confirm
      // 点击取消 返回值为： cancel
      console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$axios.get('/api/admin/deleteCurriculum/', {
        params: {
          courseId
        }
      })
      console.log(res)
      if (res !== 1) return this.$message.error('删除课程失败！')
      this.$message.success('删除课程成功！')
      this.addClassDialogVisible = false
      // 重新获取用户列表数据
      this.getCourseList()
    },
    // 根据班级id查询班级
    async getOneByClassId() {
      console.log(this.queryInfo.query)
      if (this.queryInfo.query) {
        const { data: res } = await this.$axios.get('/api/admin/getCurriculum', {
          params: {
            courseId: this.queryInfo.query
          }
        })
        console.log(res)
        this.courseList = [res]
      } else {
        this.getCourseList()
      }
    }
  }
}
</script>
